﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// PLM切片机补偿配方获取
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PLMReparationInfoController : ControllerBase
    {
        private readonly ILogger<PLMReparationInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public PLMReparationInfoController(ILogger<PLMReparationInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// PLM切片机补偿配方获取
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostPLMReparationInfo([FromBody] PLMReparationInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            PLMReparationInfoResponseDto re = new PLMReparationInfoResponseDto();
            string machine_no = strparm.machine_no;



            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select *  from iwms_sql_temp where 1=1 and  pzid='SYS0002'");
                DataTable dt_sql = DB.Query(strSql.ToString(), con);
                if (dt_sql.Rows.Count <= 0)
                {
                    strMsg = "无数据，请检查清楚再调用接口";
                }
                else
                {
                    string temp_sql = dt_sql.Rows[0]["temp_info"].ToString();

                    if (machine_no.Trim() != "")
                    {
                        temp_sql = temp_sql + " and machine_no='"+ machine_no + "'";
                    }


                    DataTable dt_ReparationInfo = DB.Query(temp_sql, con);
                    if (dt_ReparationInfo.Rows.Count>0)
                    {
                        re.data = DB.TableToListModel<PLMReparationInfoResponseData>(dt_ReparationInfo);
                    }
                    else
                    {
                        strMsg = "PLM找不到对应机台<"+ machine_no + ">有效补偿配方，请联系工艺添加或管理员";
                    }


                }     
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
